package com.bawei.shopping

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.bawei.shopping.adapter.MyFragmentAdapter
import com.bawei.shopping.databinding.ActivityMainBinding
import com.bawei.shopping.fragment.FourFragment
import com.bawei.shopping.fragment.OneFragment
import com.bawei.shopping.fragment.ThreeFragment
import com.bawei.shopping.fragment.TwoFragment
import com.bawei.shopping.widget.BottomNavigationBar
import com.bumptech.glide.Glide

/**
 1。自定义view的几种方式
 （1）自定义继承View
 （2）自定义继承ViewGroup
 （3）自定义继承现有的view或viewGroup：EditText extends TextView
 （4）组合控件：目的是复用

 2。自定义view的方法
 （1）onDraw:绘制，自定义继承View
 （2）onLayout：摆放子控件的位置，自定义继承ViewGroup
 （3）onMeasure：测量，View和ViewGroup均可重新测量
    测量规格(32位)=测量模式(高2位)+测量大小(低30位)
    测量模式：3种 AT_MOST最大值(WRAP_CONTENT)  EXACTLY精确(MATCH_PARENT和固定值)  UNSPECIFIED不确定(列表)
 */
class MainActivity : AppCompatActivity() {
    private lateinit var activityMainBinding: ActivityMainBinding
    private  var mList: MutableList<Fragment> = mutableListOf()
    private lateinit var myFragmentAdapter: MyFragmentAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(activityMainBinding.root)
        //viewpager设置适配器
        mList.add(OneFragment())
        mList.add(TwoFragment())
        mList.add(ThreeFragment())
        mList.add(FourFragment())
        myFragmentAdapter = MyFragmentAdapter(supportFragmentManager,
            FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, mList)
        activityMainBinding.viewPager.adapter = myFragmentAdapter
        //点击导航切换fragment
        activityMainBinding.bottom.setOnListener(object :BottomNavigationBar.onBottomNavClickListener{
            override fun onIconClick(viewId: Int, i: Int) {
                activityMainBinding.viewPager.currentItem = i
            }

            override fun onCenterIconClick() {
                Toast.makeText(this@MainActivity,"点击中间的",Toast.LENGTH_LONG).show()
            }

        })
        //滑动viewpager底部且魂
//        activityMainBinding.viewPager.setOnClickListener(object :View.OnClickListener{
//            override fun onClick(v: View?) {
//                TODO("Not yet implemented")
//            }
//
//        })
//        activityMainBinding.viewPager.setOnClickListener {
//
//        }
        activityMainBinding.viewPager.addOnPageChangeListener(object:ViewPager.OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                activityMainBinding.bottom.setCurrentPage(position)
            }

            override fun onPageSelected(position: Int) {

            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })


    }
}